home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / REGEXP.H < prev    next >
C/C++ Source or Header  |  1992-08-12  |  10KB  |  228 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. //
  13. // Created: MNF 06/13/89 -- Initial Design and Implementation
  14. // Updated: LGO 08/09/89 -- Inherit from Generic
  15. // Updated: MBN 09/07/89 -- Added conditional exception handling
  16. // Updated: MBN 12/15/89 -- Sprinkled "const" qualifiers all over the place!
  17. // Updated: DLS 03/22/91 -- New lite version
  18. // Updated: JAM 08/12/92 -- removed DOS specifics, stdized #includes
  19. //
  20. // This  is the header file  for the regular  expression class.   An object of
  21. // this class contains a regular expression,  in  a special "compiled" format.
  22. // This  compiled format consists  of  several slots   all kept as the objects
  23. // private data.  The  Regexp class  provides a convenient  way  to  represent
  24. // regular  expressions.  It makes it easy   to search  for  the  same regular
  25. // expression in many different strings without having to  compile a string to
  26. // regular expression format more than necessary.
  27. //
  28. // A regular  expression allows a programmer to  specify complex patterns that
  29. // can be searched for  and  matched against the  character string of a String
  30. // object.  In  its  simplest case, a   regular expression  is a  sequence  of
  31. // characters with which you can search for exact character matches.  However,
  32. // many times you may not know the exact sequence you want to find, or you may
  33. // only want to find a match at the beginning or end of  a String.  The Regexp
  34. // object  allows specification of  such patterns by  utilizing the  following
  35. // regular  expression  meta-characters   (note   that  more  one  of    these
  36. // meta-characters  can  be used in a single  regular  expression in  order to
  37. // create complex search patterns):
  38. //
  39. //         ^    Match at beginning of line
  40. //         $    Match at end of line
  41. //         .    Match any single character
  42. //         [ ]  Match any one character inside the brackets
  43. //         [^ ] Match any character NOT inside the brackets
  44. //         -    Match any character in range on either side of dash
  45. //         *    Match preceding pattern zero or more times
  46. //         +    Match preceding pattern one or more times
  47. //         ?    Match preceding pattern zero or once only
  48. //         ()   Save a matched expression and use it in a further match.
  49. //
  50. // There are three constructors for Regexp.  One  just creates an empty Regexp
  51. // object.  Another creates a Regexp object  and initializes it with a regular
  52. // expression  that is given  in  the form of a   char*.   The  third  takes a
  53. // reference  to  a Regexp  object    as an  argument    and creates an object
  54. // initialized with the information from the given Regexp object.
  55. //
  56. // The  find  member function  finds   the  first  occurence   of  the regualr
  57. // expression of that object in the string given to find as an argument.  Find
  58. // returns a boolean, and  if true,  mutates  the private  data appropriately.
  59. // Find sets pointers to the beginning and end of  the thing last  found, they
  60. // are pointers into the actual string  that was searched.   The start and end
  61. // member functions return indicies  into the searched string that  correspond
  62. // to the beginning   and  end pointers  respectively.   The    compile member
  63. // function takes a char* and puts the  compiled version of the char* argument
  64. // into the object's private data fields.  The == and  != operators only check
  65. // the  to see  if   the compiled  regular  expression   is the same, and  the
  66. // deep_equal functions also checks  to see if the  start and end pointers are
  67. // the same.  The is_valid  function returns FALSE if  program is set to NULL,
  68. // (i.e. there is no valid compiled exression).  The set_invalid function sets
  69. // the  program to NULL  (Warning: this deletes the compiled  expression). The
  70. // following examples may help clarify regular expression usage:
  71. //
  72. //   *  The regular expression  "^hello" matches  a "hello"  only at  the
  73. //      beginning of a  line.  It would match "hello  there" but not "hi,
  74. //      hello there".
  75. //
  76. //   *  The regular expression "long$" matches a  "long"  only at the end
  77. //      of a line. It would match "so long\0", but not "long ago".
  78. //
  79. //   *  The regular expression "t..t..g"  will match anything that  has a
  80. //      "t" then any two characters, another "t", any  two characters and
  81. //      then a "g".   It will match  "testing", or "test again" but would
  82. //      not match "toasting"
  83. //
  84. //   *  The regular  expression "[1-9ab]" matches any  number one through
  85. //      nine, and the characters  "a" and  "b".  It would match "hello 1"
  86. //      or "begin", but would not match "no-match".
  87. //
  88. //   *  The  regular expression "[^1-9ab]"  matches any character that is
  89. //      not a number one  through nine, or  an "a" or "b".   It would NOT
  90. //      match "hello 1" or "begin", but would match "no-match".
  91. //
  92. //   *  The regular expression "br* " matches  something that begins with
  93. //      a "b", is followed by zero or more "r"s, and ends in a space.  It
  94. //      would match "brrrrr ", and "b ", but would not match "brrh ".
  95. //
  96. //   *  The regular expression "br+ " matches something  that begins with
  97. //      a "b", is followed by one or more "r"s, and ends in  a space.  It
  98. //      would match "brrrrr ",  and  "br ", but would not  match "b  " or
  99. //      "brrh ".
  100. //
  101. //   *  The regular expression "br? " matches  something that begins with
  102. //      a "b", is followed by zero or one "r"s, and ends in  a space.  It
  103. //      would  match  "br ", and "b  ", but would not match  "brrrr "  or
  104. //      "brrh ".
  105. //
  106. //   *  The regular expression "(..p)b" matches  something ending with pb
  107. //      and beginning with whatever the two characters before the first p
  108. //      encounterd in the line were.  It would find  "repb" in "rep drepa
  109. //      qrepb".  The regular expression "(..p)a"  would find "repa qrepb"
  110. //      in "rep drepa qrepb"
  111. //
  112. //   *  The regular expression "d(..p)" matches something ending  with p,
  113. //      beginning with d, and having  two characters  in between that are
  114. //      the same as the two characters before  the first p  encounterd in
  115. //      the line.  It would match "drepa qrepb" in "rep drepa qrepb".
  116. //
  117.  
  118. #ifndef REGEXPH                    // If no regular expression
  119. #define REGEXPH
  120.  
  121. #ifndef CHARH
  122. #include <cool/char.h>
  123. #endif
  124.  
  125. #define MAGIC 0234
  126. #define NSUBEXP 10
  127.  
  128. class CoolRegexp {
  129. public:
  130.   inline CoolRegexp ();                // CoolRegexp with program=NULL
  131.   inline CoolRegexp (const char*);        // CoolRegexp with compiled char*
  132.   CoolRegexp (const CoolRegexp&);        // Copy constructor
  133.   ~CoolRegexp();                // Destructor 
  134.  
  135.   void compile (const char*);            // Compiles char* --> regexp
  136.   Boolean find (const char*);            // TRUE if regexp in char* arg
  137.   inline long start() const;            // Index to start of first find
  138.   inline long end() const;            // Index to end of first find
  139.  
  140.   Boolean operator== (const CoolRegexp&) const;    // Equality operator
  141.   inline Boolean operator!= (const CoolRegexp&) const; // Inequality operator
  142.   Boolean deep_equal (const CoolRegexp&) const;    // Same regexp and state?
  143.   
  144.   inline Boolean is_valid() const;        // TRUE if compiled regexp
  145.   inline void set_invalid();            // Invalidates regexp
  146.  
  147. private: 
  148.   const char* startp[NSUBEXP];
  149.   const char* endp[NSUBEXP];
  150.         char  regstart;            // Internal use only
  151.         char  reganch;            // Internal use only
  152.   const char* regmust;            // Internal use only
  153.         int   regmlen;            // Internal use only
  154.         char* program;   
  155.         int   progsize;
  156.   const char* searchstring;
  157. }; 
  158.  
  159. // CoolRegexp -- Simple constructor
  160. // Input:    None
  161. // Output:   None
  162.  
  163. inline CoolRegexp::CoolRegexp () { 
  164.   this->program = NULL;
  165. }
  166.  
  167.  
  168. // CoolRegexp -- Constructor that accepts a string argument and compiles as a
  169. //           regular expression
  170. // Input:    Character string form of regular expression
  171. // Output:   None
  172.  
  173. inline CoolRegexp::CoolRegexp (const char* s) {  
  174.   this->program = NULL;
  175.   compile(s);
  176. }
  177.  
  178.  
  179. // Start -- Returns index into the searched string that corresponds to the
  180. //          beginning pointer
  181. // Input:   None
  182. // Output:  Long index into searched string
  183.  
  184. inline long CoolRegexp::start () const {
  185.   return(this->startp[0] - searchstring);
  186. }
  187.  
  188.  
  189. // End -- Returns index into the searched string that corresponds to the
  190. //        end pointer
  191. // Input: None
  192. // Output: Long index into searched string
  193.  
  194. inline long CoolRegexp::end () const {
  195.   return(this->endp[0] - searchstring);
  196. }
  197.  
  198.  
  199. // operator!= -- Overload inequality operator for CoolRegexp class
  200. // Input:        Constance CoolRegexp reference
  201. // Output:       Boolean TRUE/FALSE
  202.  
  203. inline Boolean CoolRegexp::operator!= (const CoolRegexp& r) const {
  204.   return(!(*this == r));
  205. }
  206.  
  207.  
  208. // is_valid -- Indicates validity of regular expression, ie. is there a
  209. //             compiled regular expression
  210. // Input:      None
  211. // Output:     Boolean TRUE/FALSE
  212.  
  213. inline Boolean CoolRegexp::is_valid () const {
  214.   return (this->program != NULL);
  215. }
  216.  
  217.  
  218. // set_invalid -- Invalidates regular expression
  219. // Input:         None
  220. // Output:        None
  221.  
  222. inline void CoolRegexp::set_invalid () {
  223.   delete [] this->program;
  224.   this->program = NULL;
  225. }
  226.  
  227. #endif
  228.